package com.example.util;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
/**
 * Elasticsearch工具类（未完成）
* @ClassName: ElasticsearchUtil    
* @Description:     
* @author xzx   
* @date 2018年3月6日 下午6:08:25    
*
 */
public class ElasticsearchUtil {
	

/*	private  ElasticsearchUtil() throws UnknownHostException{
		


//		Settings esSettings = Settings.builder()
//				.put("client.transport.sniff", true)
//				.put("cluster.name", "elasticsearch").build();//自动嗅探整个集群的状态，把集群中其他ES节点的ip添加到本地的客户端列表中
//		client = new PreBuiltTransportClient(esSettings);//初始化client较老版本发生了变化，此方法有几个重载方法，初始化插件等。
//				//此步骤添加IP，至少一个，其实一个就够了，因为添加了自动嗅探配置
//				client.addTransportAddress(new TransportAddress(InetAddress.getByName("10.10.9.33"), 9200));
//				String json = "{}";
//						IndexResponse response = client.prepareIndex("ylzx", "customer")
//						.setSource(json)
//						.get();
//						System.out.println(response.getResult());
	}*/
	
	
	   /*  public  TransportClient getConnection() throws UnknownHostException{
		            if (client==null){
		                synchronized (ElasticsearchUtil.class){
		                  if (client==null){
		                        new ElasticsearchUtil();
		                 }
		             }
		         }
		          return  client;
	     }*/
	
	public static void main(String[] args) throws IOException {
		long start = new Date().getTime();
		TransportClient client = null;
		Settings settings = Settings.builder().put("cluster.name", "my-application").put("client.transport.sniff", true).build();
        
        client = new PreBuiltTransportClient(settings)
        .addTransportAddress(new TransportAddress(InetAddress.getByName("ehost"), 9300));
		String json = "{}";
		XContentParser parser = XContentFactory.xContent(XContentType.JSON). createParser(NamedXContentRegistry.EMPTY, json);
		SearchRequest request = new SearchRequest();
//		client.prepareSearch("ylzx").setSearchType("customer").setQuery(queryBuilder);.setQuery(QueryBuilders.matchQuery("ylzx", "customer"));
//		GetRequestBuilder  util = client.prepareGet().setIndex("ylzx").setType("customer");
		SearchRequestBuilder requestBuilder = client.prepareSearch("ylzx").setTypes("customer").setQuery(QueryBuilders.matchAllQuery());
		for(int i=0; i<9; i++){
		    SearchResponse response = requestBuilder.setFrom(i).setSize(10).execute().actionGet();
		    System.out.println("总量"+0+" 已经查到");
		}
		
		
		System.out.println(requestBuilder.setFrom(0).setSize(50000).execute().actionGet());
		long end = new Date().getTime();
		System.out.println(end-start);
	}

}
